package com.order.common.entiry;

import com.alibaba.fastjson.JSON;
import lombok.Data;

@Data
public class ResultEntity {
    private boolean success;
    private Integer code;
    private String msg;
    private Object data;

    public ResultEntity() {

    }

    public ResultEntity(boolean success, Integer code, String msg, Object data) {
        this.success = success;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ResultEntity(boolean success, Integer code, String msg) {
        this.success = success;
        this.code = code;
        this.msg = msg;
    }

    public ResultEntity(ResultCode resultCode) {
        this.success = resultCode.isSuccess();
        this.code = resultCode.getCode();
        this.msg = resultCode.getMsg();
    }

    public ResultEntity(ResultCode resultCode, Object data) {
        this.success = resultCode.isSuccess();
        this.code = resultCode.getCode();
        this.msg = resultCode.getMsg();
        this.data = data;
    }


    public static  ResultEntity SUCCESS() {
        return new ResultEntity(ResultCode.SUCCESS);
    }

    public static  ResultEntity ERROR() {
        return new ResultEntity(ResultCode.SERVER_ERROR);
    }

    public static ResultEntity ERROR(String msg) {
        ResultEntity result = new ResultEntity();
        result.setCode(500);
        result.setMsg(msg);
        return result;
    }

    public static  ResultEntity FAIL() {
        return new ResultEntity(ResultCode.FAIL);
    }

    public String toJson() {
        return JSON.toJSONString(this);
    }
}
